package com.wontube.smr.remote.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.annotation.CacheSupport;
import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.pub.servlet.handlers.BasicHandler;
import com.wontube.smr.bo.LocationBO;
import com.wontube.smr.client.model.po.Country;
import com.wontube.smr.client.model.po.MVChart;
import com.wontube.smr.client.model.po.TVEpisode;
import com.wontube.smr.client.model.po.TVShow;
import com.wontube.smr.client.model.vo.LocationInfo;
import com.wontube.smr.client.model.vo.MVChartItems;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.client.model.vo.SeasonEpisodes;
import com.wontube.smr.client.model.vo.TVShowItems;
import com.wontube.smr.dao.bo.AnimeSelectDao;
import com.wontube.smr.dao.bo.HomeBannerSelectDao;
import com.wontube.smr.dao.bo.MVSelectDao;
import com.wontube.smr.dao.bo.MovieSelectDao;
import com.wontube.smr.dao.bo.TVSelectDao;
import com.wontube.smr.web.client.IWebHomeHandler;
import com.wontube.smr.web.client.model.bo.QueryItemParams;
import com.wontube.smr.web.client.model.po.HomeBanner;

/**
 * @author yuechao
 * 
 */
@NoLoginSupport
public final class WebHomeHandler extends BasicHandler implements IWebHomeHandler {

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public Map<String, Object> listI18nWebHome() {
		Map<String, Object> results = new HashMap<String, Object>();
		QueryItemParams params = new QueryItemParams();
		params.setSort("time");
		MovieItems movieItems = MovieSelectDao.selectMovieImdbTraillers(params, new PagenateInfo(1, 6));
		results.put("movieItems", movieItems);

		TVShowItems tvItems = AnimeSelectDao.selectRecentAnimes(null, new PagenateInfo(1, 3));
		List<TVEpisode> episodeList = new ArrayList<TVEpisode>();
		for (TVShow tvShow : tvItems.getItems()) {
			List<SeasonEpisodes> seasons = TVSelectDao.getAllEpisodes(tvShow.getId());
			List<TVEpisode> temp = seasons.get(seasons.size() - 1).getEpisodes();
			if (temp.size() > 0) {
				TVEpisode tv = temp.get(temp.size() - 2);
				episodeList.add(tv);
			}
		}
		results.put("animeItems", episodeList);

		MVChartItems ytweeklyItems = MVSelectDao.selectMVCharts(MVChart.CATEGORY_YOUTUBE_TOP,
				MVChart.UPDATE_PERIOD_WEEKLY, new PagenateInfo(1, 3));
		results.put("musicItems", ytweeklyItems);

		return results;
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public Map<String, Object> listWebHome() {
		Map<String, Object> results = new HashMap<String, Object>();
		
		MovieItems movieItems = MovieSelectDao.selectHuluPopularMovies(new PagenateInfo(1, 8));
		results.put("movieItems", movieItems);

		TVShowItems tvItems = TVSelectDao.selectHuluPopularShows(new PagenateInfo(1, 8));
		results.put("tvItems", tvItems);

		MovieItems musicItems = MovieSelectDao.selectVevoMusics(new QueryItemParams(), new PagenateInfo(1, 8));
		results.put("musicItems", musicItems);

		MVChartItems topMusicItems = MVSelectDao.selectMVCharts(MVChart.CATEGORY_VEVO_TOP_VIDEOS,
				MVChart.UPDATE_PERIOD_DAILY, new PagenateInfo(1, 10));
		results.put("topMusicItems", topMusicItems);

		MovieItems inTheaterItems = MovieSelectDao.selectInTheaterTrailers(null, new PagenateInfo(1, 5));
		results.put("inTheaterItems", inTheaterItems);

		MovieItems comingSoonItems = MovieSelectDao.selectComingSoonTrailers(null, new PagenateInfo(1, 5));
		results.put("comingSoonItems", comingSoonItems);

		List<HomeBanner> homeBannerList = HomeBannerSelectDao.listHomeBanner();
		results.put("homeBannerList", homeBannerList);

		return results;
	}

	@Override
	public Boolean isFromWorldWide() {
		// 从ip判断hl
		String ip = super.getContext().getSession().getLastClientIP();
		
		LocationInfo location = LocationBO.getLocation(ip);
		
		if (location == null) {		
			logger.debug("cannot get location info for ip[{}]", ip);			
			return true;
		} 		

		String code = location.getCountryCode();
		logger.debug("ip[{}] from [{}],[{}]", new Object[]{ip, location.getCountryName(), code});		
		if (Country.US_COUNTRY_CODE.equals(code)) {
			return false;
		}
		
		return true;		
	}
}
